package py.com.alfa.escriba.gui.rest.services.resttemplate;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import py.com.alfa.escriba.gui.config.EscribaConfigApplicationContextProvider;
import py.com.alfa.escriba.gui.dto.gestion.VehiculosDTO;
import py.com.alfa.escriba.gui.dto.gestion.VehiculosinfoDTO;
import py.com.alfa.escriba.gui.rest.services.VehiculosService;
import py.com.alfa.escriba.gui.services.Services;
import py.com.sif.rest.client.spring.request.ClientRequestBuilder;
@SuppressWarnings({ "unchecked"})
public class VehiculosServiceClient implements VehiculosService {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	public VehiculosServiceClient() {}

	@Override
	public List<VehiculosDTO> listVehiculos() throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		ResponseEntity<VehiculosDTO[]> response = (ResponseEntity<VehiculosDTO[]>) request.exchange(LIST_VEHI, HttpMethod.GET, VehiculosDTO[].class);
		log.info("OK - listVehiculos ");
		List<VehiculosDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}
	
	@Override
	public List<VehiculosDTO> buscarVehiculos(VehiculosDTO vehiculo) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(vehiculo);
		ResponseEntity<VehiculosDTO[]> response = (ResponseEntity<VehiculosDTO[]>) request.exchange(BUS_VEHI, HttpMethod.POST, VehiculosDTO[].class);
		log.info("OK - buscarVehiculos ");
		List<VehiculosDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public VehiculosDTO createVehiculo(VehiculosDTO vehiculo)
			throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(vehiculo);
		ResponseEntity<VehiculosDTO> response = (ResponseEntity<VehiculosDTO>) request.exchange(CREA_VEHI, HttpMethod.POST, VehiculosDTO.class);
		log.info("OK - createVehiculo ");
		VehiculosDTO result = response.getBody();
		return result;
	}

	@Override
	public VehiculosDTO saveVehiculo(VehiculosDTO vehiculo) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", vehiculo.getId().toString());
		request.addBody(vehiculo);
		ResponseEntity<VehiculosDTO> response = (ResponseEntity<VehiculosDTO>) request.exchange(UPD_VEHI, HttpMethod.PUT, VehiculosDTO.class);
		log.info("OK - saveVehiculo ");
		VehiculosDTO result = response.getBody();
		return result;
	}

	@Override
	public void deleteVehiculo(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		request.exchange(DEL_VEHI, HttpMethod.DELETE, null);
		log.info("OK - deleteVehiculo ");

	}

	@Override
	public VehiculosDTO showVehiculo(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<VehiculosDTO> response = (ResponseEntity<VehiculosDTO>) request.exchange(GET_VEHI, HttpMethod.GET, VehiculosDTO.class);
		log.info("OK - showVehiculo ");
		VehiculosDTO result = response.getBody();
		return result;
	}
	
	@Override
	public List<VehiculosinfoDTO> listDatosVehiculosInfo() throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		ResponseEntity<VehiculosinfoDTO[]> response = (ResponseEntity<VehiculosinfoDTO[]>) request.exchange(LIST_VEHI_INFO, HttpMethod.GET, VehiculosinfoDTO[].class);
		log.info("OK - listDatosVehiculosInfo ");
		List<VehiculosinfoDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

}
